package com.youkeyi.ddy.cloud.basic.pub.todo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.todo.entity.SysUserTodo;
import com.youkeyi.ddy.cloud.basic.pub.todo.service.SysUserTodoService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.PublicTodoCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.dto.PublicTodoListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.dto.PublicTodoListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import com.youkeyi.ddy.cloud.common.utils.SystemDateUtils;
import com.youkeyi.ddy.cloud.common.wx.applet.message.service.WxMessageService;
import com.youkeyi.ddy.cloud.common.wx.applet.message.utils.WxTemplateMessageSendUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 用户待办表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-01
 */
@Service
public class SysUserTodoController implements PublicTodoCloudService {
    
    @Autowired
    private SysUserTodoService sysUserTodoService;
    @Autowired
    private WxMessageService wxMessageService;

    @Override
    public PublicTodoListCloudDto query(PublicTodoQueryCloudParam param) {
        PublicTodoListCloudDto dto = new PublicTodoListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getTodoPrimaryKeys())) {
            param.getTodoPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getStatuses())) {
            param.getStatuses().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getUserKeys())) {
            param.getUserKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCustomerKeys())) {
            param.getCustomerKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getTypes())) {
            param.getTypes().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysUserTodo> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getTodoPrimaryKeys()), SysUserTodo::getPrimaryKey, param.getTodoPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysUserTodo::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getUserKeys()), SysUserTodo::getUserKey, param.getUserKeys())
                .in(CollectionUtils.isNotEmpty(param.getCustomerKeys()), SysUserTodo::getCustomerKey, param.getCustomerKeys())
                .in(CollectionUtils.isNotEmpty(param.getStatuses()), SysUserTodo::getStatus, param.getStatuses())
                .in(CollectionUtils.isNotEmpty(param.getTypes()), SysUserTodo::getType, param.getTypes())
                .like(StringUtils.isNotBlank(param.getContentFuzzy()), SysUserTodo::getContent, param.getContentFuzzy())
                .ne(SysUserTodo::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysUserTodo> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysUserTodo> todoList = sysUserTodoService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(todoList.getRecords())) {
            dto.setTotal(todoList.getTotal());

            List<PublicTodoListItemCloudDto> items = new ArrayList<>();
            todoList.getRecords().forEach(todo -> {
                PublicTodoListItemCloudDto item = new PublicTodoListItemCloudDto();
                item.setTodoPrimaryKey(todo.getPrimaryKey());
                item.setCorpKey(todo.getCorpKey());
                item.setType(todo.getType());
                item.setTypeName(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_TODO_TYPE.getName(todo.getType()));
                item.setUserKey(todo.getUserKey());
                item.setCustomerKey(todo.getCustomerKey());
                item.setContent(todo.getContent());
                item.setStatus(todo.getStatus());
                item.setFinishTime(todo.getFinishTime() != null ? todo.getFinishTime().getTime() : null);
                item.setRemark(todo.getRemark());
                item.setIsDeleted(todo.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(todo.getCreateTime() != null ? todo.getCreateTime().getTime() : null);
                item.setCreateUserKey(todo.getCreateUserKey());
                item.setCreateUserName(todo.getCreateUserName());
                item.setUpdateTime(todo.getUpdateTime() != null ? todo.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(todo.getUpdateUserKey());
                item.setUpdateUserName(todo.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(PublicTodoCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getUserKey()) || StringUtils.isBlank(param.getContent())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysUserTodo todo = new SysUserTodo();
        todo.setPrimaryKey(SimpleUUidUtils.getUuid());
        todo.setCorpKey(param.getCorpKey());
        todo.setType(param.getType());
        todo.setUserKey(param.getUserKey());
        todo.setCustomerKey(param.getCustomerKey());
        todo.setContent(param.getContent());
        todo.setStatus(param.getStatus());
        todo.setFinishTime(param.getFinishTime() != null ? new Date(param.getFinishTime()) : null);
        todo.setRemark(param.getRemark());
        todo.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        todo.setCreateUserKey(param.getCreateUserKey());
        todo.setCreateUserName(param.getCreateUserName());
        todo.setCreateTime(new Date());
        boolean save = sysUserTodoService.save(todo);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        if (null != param.getSendWxMessage() && param.getSendWxMessage() && 1 == param.getStatus()) {
            wxMessageService.subscribeSendForTodo(
                    Collections.singletonList(param.getUserKey()),
                    param.getContent(),
                    param.getCreateUserName(),
                    "待办",
                    todo.getCreateTime().getTime()
            );
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(todo.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(PublicTodoUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getTodoPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysUserTodo> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysUserTodo::getPrimaryKey, param.getTodoPrimaryKey());
        SysUserTodo todo = new SysUserTodo();
        todo.setPrimaryKey(param.getTodoPrimaryKey());
        todo.setContent(param.getContent());
        todo.setStatus(param.getStatus());
        todo.setFinishTime(param.getFinishTime() != null ? new Date(param.getFinishTime()) : null);
        todo.setRemark(param.getRemark());
        todo.setUpdateUserKey(param.getUpdateUserKey());
        todo.setUpdateUserName(param.getUpdateUserName());
        todo.setUpdateTime(new Date());
        boolean save = sysUserTodoService.update(todo, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(PublicTodoDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getTodoPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysUserTodo> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysUserTodo::getPrimaryKey, param.getTodoPrimaryKeys());

        SysUserTodo todo = new SysUserTodo();
        todo.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        todo.setUpdateUserKey(param.getUpdateUserKey());
        todo.setUpdateUserName(param.getUpdateUserName());
        todo.setUpdateTime(new Date());
        boolean save = sysUserTodoService.update(todo, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
